package com.neulogistics.contrib.transfercenter.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.transfercenter.api.application.command.GetSourceWarehouseIdCommand;
import com.neulogistics.contrib.transfercenter.api.application.query.ITransferOrderQueryService;
import com.neulogistics.contrib.transfercenter.api.application.query.TransferOrderQuery;
import com.neulogistics.contrib.transfercenter.api.application.viewmodel.TransferOrderViewModel;
import com.neulogistics.contrib.transfercenter.domain.model.aggregate.TransferOrder;
import com.neulogistics.contrib.transfercenter.domain.repository.ITransferOrderRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "调度单管理")
@RequestMapping("/transferCenter/transferOrder")
public class TransferOrderController {
    @Resource
    IMediator mediator;

    @Resource
    ITransferOrderRepository transferOrderRepository;

    @Resource
    ITransferOrderQueryService transferOrderQueryService;


    @ApiOperation("获取对应订单id")
    @GetMapping("/getOrderIdByTransfer/{transferId}")
    public BaseResponse<Long> getOrderIdByTransfer(@PathVariable @NotNull @Positive Long transferId){
        TransferOrder transferOrder = transferOrderRepository.findById(transferId);
        Long orderId = transferOrder.getOrderId();
        return ResultUtils.success(orderId);
    }

    @ApiOperation("根据id获取调度单")
    @GetMapping("/get/{id}")
    public BaseResponse<TransferOrderViewModel> get(@PathVariable(value = "id") @NotNull @Positive Long id){

        TransferOrderViewModel transferOrderViewModel = transferOrderQueryService.getById(id);

        return ResultUtils.success(transferOrderViewModel);
    }

    @ApiOperation("分页查询调度单")
    @PostMapping("/page")
    public BaseResponse<Page<TransferOrderViewModel>> page(@RequestBody @Validated @NotNull TransferOrderQuery transferOrderQuery){


        Page<TransferOrderViewModel> viewModelPage = transferOrderQueryService.page(transferOrderQuery);

        return ResultUtils.success(viewModelPage);
    }

    @ApiOperation("根据调拨单id查询出入库来源库房id")
    @PostMapping("/getSourceWarehouseId")
    public BaseResponse<Integer> getSourceWarehouseId(@RequestBody @NotNull GetSourceWarehouseIdCommand command){
        BaseResponse<Integer> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }
}
